package assignment7;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

/**
 * Contains all methods associated with the GUI
 * 
 * @author Terry Wasserleben CS204-21370 Prof. Jeannette Myers
 */
public class ConnectionsPanel extends JPanel {
	
	private static final long serialVersionUID = 8448803657678095435L;
	
	private String profileSelected;
	
	private JButton btnExit;
	private JButton btnAddFriend;
	private JButton btnAddProfile;
	private JButton btnShowFriends;
	
	private JTextField profFNameJTF;
	private JTextField profLNameJTF;
	private JTextField profHtownJTF;
	private JTextField newProfFNameJTF;
	private JTextField newProfLNameJTF;
	private JTextField newProfHtownJTF;
	
	private JComboBox profileJCB;
	
	private JList fofJList;
	
	private DataManager dm = new DataManager();
	
	/**
	 * Constructs the Connections Panel
	 */
	public ConnectionsPanel() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] { 0, 0, 0, 0, 0 };
		gridBagLayout.rowHeights = new int[] { 40, 0, 0, 0, 40, 0 };
		gridBagLayout.columnWeights = new double[] { 0.0, 1.0, 1.0, 0.0,
				Double.MIN_VALUE };
		gridBagLayout.rowWeights = new double[] { 0.0, 1.0, 1.0, 1.0, 0.0,
				Double.MIN_VALUE };
		setLayout(gridBagLayout);
		
		JLabel headerJL = new JLabel("Connections");
		GridBagConstraints gbc_headerJL = new GridBagConstraints();
		gbc_headerJL.gridwidth = 2;
		gbc_headerJL.insets = new Insets(0, 0, 5, 5);
		gbc_headerJL.gridx = 1;
		gbc_headerJL.gridy = 0;
		add(headerJL, gbc_headerJL);
		
		JPanel profJP = new JPanel();
		profJP.setBorder(new TitledBorder(new EtchedBorder(
				EtchedBorder.LOWERED, null, null), "Choose a Profile",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GridBagConstraints gbc_profJP = new GridBagConstraints();
		gbc_profJP.gridheight = 2;
		gbc_profJP.insets = new Insets(0, 0, 5, 5);
		gbc_profJP.fill = GridBagConstraints.BOTH;
		gbc_profJP.gridx = 1;
		gbc_profJP.gridy = 1;
		add(profJP, gbc_profJP);
		GridBagLayout gbl_profJP = new GridBagLayout();
		gbl_profJP.columnWidths = new int[] { 0, 76, 114, 0, 0 };
		gbl_profJP.rowHeights = new int[] { 24, 19, 19, 0, 0, 0, 0 };
		gbl_profJP.columnWeights = new double[] { 0.0, 0.0, 1.0, 0.0,
				Double.MIN_VALUE };
		gbl_profJP.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		profJP.setLayout(gbl_profJP);
		
		profileJCB = new JComboBox();
		profileJCB.setMaximumRowCount(6);
		profileJCB.setAlignmentX(Component.LEFT_ALIGNMENT);
		profileJCB.addActionListener(new boxListener());
		GridBagConstraints gbc_profileJCB = new GridBagConstraints();
		gbc_profileJCB.anchor = GridBagConstraints.NORTH;
		gbc_profileJCB.insets = new Insets(0, 0, 5, 5);
		gbc_profileJCB.gridx = 2;
		gbc_profileJCB.gridy = 0;
		profJP.add(profileJCB, gbc_profileJCB);
		
		JLabel profileFNameJL = new JLabel("First Name");
		GridBagConstraints gbc_profileFNameJL = new GridBagConstraints();
		gbc_profileFNameJL.anchor = GridBagConstraints.NORTHWEST;
		gbc_profileFNameJL.insets = new Insets(0, 0, 5, 5);
		gbc_profileFNameJL.gridx = 1;
		gbc_profileFNameJL.gridy = 1;
		profJP.add(profileFNameJL, gbc_profileFNameJL);
		
		profFNameJTF = new JTextField();
		profFNameJTF.setColumns(15);
		GridBagConstraints gbc_profFNameJTF = new GridBagConstraints();
		gbc_profFNameJTF.anchor = GridBagConstraints.NORTHWEST;
		gbc_profFNameJTF.insets = new Insets(0, 0, 5, 5);
		gbc_profFNameJTF.gridx = 2;
		gbc_profFNameJTF.gridy = 1;
		profJP.add(profFNameJTF, gbc_profFNameJTF);
		
		JLabel profileLNameJL = new JLabel("Last Name");
		GridBagConstraints gbc_profileLNameJL = new GridBagConstraints();
		gbc_profileLNameJL.anchor = GridBagConstraints.WEST;
		gbc_profileLNameJL.insets = new Insets(0, 0, 5, 5);
		gbc_profileLNameJL.gridx = 1;
		gbc_profileLNameJL.gridy = 2;
		profJP.add(profileLNameJL, gbc_profileLNameJL);
		
		profLNameJTF = new JTextField();
		GridBagConstraints gbc_profLNameJTF = new GridBagConstraints();
		gbc_profLNameJTF.insets = new Insets(0, 0, 5, 5);
		gbc_profLNameJTF.anchor = GridBagConstraints.NORTHWEST;
		gbc_profLNameJTF.gridx = 2;
		gbc_profLNameJTF.gridy = 2;
		profJP.add(profLNameJTF, gbc_profLNameJTF);
		profLNameJTF.setColumns(15);
		
		JLabel lblHometown = new JLabel("Hometown");
		GridBagConstraints gbc_lblHometown = new GridBagConstraints();
		gbc_lblHometown.anchor = GridBagConstraints.EAST;
		gbc_lblHometown.insets = new Insets(0, 0, 5, 5);
		gbc_lblHometown.gridx = 1;
		gbc_lblHometown.gridy = 3;
		profJP.add(lblHometown, gbc_lblHometown);
		
		profHtownJTF = new JTextField();
		GridBagConstraints gbc_profHTownJTF = new GridBagConstraints();
		gbc_profHTownJTF.insets = new Insets(0, 0, 5, 5);
		gbc_profHTownJTF.fill = GridBagConstraints.HORIZONTAL;
		gbc_profHTownJTF.gridx = 2;
		gbc_profHTownJTF.gridy = 3;
		profJP.add(profHtownJTF, gbc_profHTownJTF);
		profHtownJTF.setColumns(15);
		
		JList profFriendsJList = new JList();
		profFriendsJList.setPreferredSize(new Dimension(120, 120));
		profFriendsJList.setMinimumSize(new Dimension(120, 120));
		profFriendsJList.setBorder(new TitledBorder(new EtchedBorder(
				EtchedBorder.LOWERED, null, null), "Friends",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GridBagConstraints gbc_profFriendsJList = new GridBagConstraints();
		gbc_profFriendsJList.insets = new Insets(0, 0, 5, 5);
		gbc_profFriendsJList.gridwidth = 2;
		gbc_profFriendsJList.fill = GridBagConstraints.BOTH;
		gbc_profFriendsJList.gridx = 1;
		gbc_profFriendsJList.gridy = 4;
		profJP.add(profFriendsJList, gbc_profFriendsJList);
		
		JPanel fofJP = new JPanel();
		fofJP.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED,
				null, null), "Friends of Friends", TitledBorder.LEADING,
				TitledBorder.TOP, null, null));
		GridBagConstraints gbc_fofJP = new GridBagConstraints();
		gbc_fofJP.insets = new Insets(0, 0, 5, 5);
		gbc_fofJP.fill = GridBagConstraints.BOTH;
		gbc_fofJP.gridx = 2;
		gbc_fofJP.gridy = 1;
		add(fofJP, gbc_fofJP);
		GridBagLayout gbl_fofJP = new GridBagLayout();
		gbl_fofJP.columnWidths = new int[] { 28, 0, 0, 23, 0, 0 };
		gbl_fofJP.rowHeights = new int[] { 0, 0, 0, 0 };
		gbl_fofJP.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		gbl_fofJP.rowWeights = new double[] { 0.0, 1.0, 0.0, Double.MIN_VALUE };
		fofJP.setLayout(gbl_fofJP);
		
		btnShowFriends = new JButton("Show Friends of Friends");
		btnShowFriends.addActionListener(new btnListener());
		GridBagConstraints gbc_btnShowFriends = new GridBagConstraints();
		gbc_btnShowFriends.insets = new Insets(0, 0, 5, 5);
		gbc_btnShowFriends.gridx = 2;
		gbc_btnShowFriends.gridy = 0;
		fofJP.add(btnShowFriends, gbc_btnShowFriends);
		
		fofJList = new JList();
		fofJList.setMinimumSize(new Dimension(120, 60));
		fofJList.setPreferredSize(new Dimension(120, 60));
		GridBagConstraints gbc_fofJList = new GridBagConstraints();
		gbc_fofJList.gridwidth = 3;
		gbc_fofJList.insets = new Insets(0, 0, 5, 5);
		gbc_fofJList.fill = GridBagConstraints.BOTH;
		gbc_fofJList.gridx = 1;
		gbc_fofJList.gridy = 1;
		fofJP.add(fofJList, gbc_fofJList);
		
		JPanel addProfJP = new JPanel();
		addProfJP.setBorder(new TitledBorder(new EtchedBorder(
				EtchedBorder.LOWERED, null, null), "Add A Friend",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GridBagConstraints gbc_addProfJP = new GridBagConstraints();
		gbc_addProfJP.insets = new Insets(0, 0, 5, 5);
		gbc_addProfJP.fill = GridBagConstraints.BOTH;
		gbc_addProfJP.gridx = 2;
		gbc_addProfJP.gridy = 2;
		add(addProfJP, gbc_addProfJP);
		GridBagLayout gbl_addProfJP = new GridBagLayout();
		gbl_addProfJP.columnWidths = new int[] { 0, 32, 110, 0, 0 };
		gbl_addProfJP.rowHeights = new int[] { 25, 0, 0 };
		gbl_addProfJP.columnWeights = new double[] { 1.0, 0.0, 0.0, 1.0,
				Double.MIN_VALUE };
		gbl_addProfJP.rowWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
		addProfJP.setLayout(gbl_addProfJP);
		
		btnAddFriend = new JButton("Add Friend");
		btnAddFriend.addActionListener(new btnListener());
		
		JComboBox addFriendJCB = new JComboBox();
		GridBagConstraints gbc_addFriendJCB = new GridBagConstraints();
		gbc_addFriendJCB.anchor = GridBagConstraints.NORTH;
		gbc_addFriendJCB.insets = new Insets(0, 0, 0, 5);
		gbc_addFriendJCB.gridx = 1;
		gbc_addFriendJCB.gridy = 1;
		addProfJP.add(addFriendJCB, gbc_addFriendJCB);
		GridBagConstraints gbc_btnAddFriend = new GridBagConstraints();
		gbc_btnAddFriend.insets = new Insets(0, 0, 0, 5);
		gbc_btnAddFriend.anchor = GridBagConstraints.NORTH;
		gbc_btnAddFriend.gridx = 2;
		gbc_btnAddFriend.gridy = 1;
		addProfJP.add(btnAddFriend, gbc_btnAddFriend);
		
		JPanel newProfJP = new JPanel();
		newProfJP.setBorder(new TitledBorder(new EtchedBorder(
				EtchedBorder.LOWERED, null, null), "Create New Profile",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GridBagConstraints gbc_newProfJP = new GridBagConstraints();
		gbc_newProfJP.gridwidth = 2;
		gbc_newProfJP.insets = new Insets(0, 0, 5, 5);
		gbc_newProfJP.fill = GridBagConstraints.BOTH;
		gbc_newProfJP.gridx = 1;
		gbc_newProfJP.gridy = 3;
		add(newProfJP, gbc_newProfJP);
		GridBagLayout gbl_newProfJP = new GridBagLayout();
		gbl_newProfJP.columnWidths = new int[] { 0, 0, 155, 76, 0, 0, 0, 0, 0 };
		gbl_newProfJP.rowHeights = new int[] { 0, 15, 0, 0, 0, 0 };
		gbl_newProfJP.columnWeights = new double[] { 0.0, 0.0, 0.0, 1.0, 0.0,
				0.0, 0.0, 0.0, Double.MIN_VALUE };
		gbl_newProfJP.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		newProfJP.setLayout(gbl_newProfJP);
		
		JLabel createFNameJL = new JLabel("Frist Name");
		createFNameJL.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_createFNameJL = new GridBagConstraints();
		gbc_createFNameJL.insets = new Insets(0, 0, 5, 5);
		gbc_createFNameJL.anchor = GridBagConstraints.NORTHEAST;
		gbc_createFNameJL.gridx = 2;
		gbc_createFNameJL.gridy = 1;
		newProfJP.add(createFNameJL, gbc_createFNameJL);
		
		newProfFNameJTF = new JTextField();
		GridBagConstraints gbc_NewProfFNameJTF = new GridBagConstraints();
		gbc_NewProfFNameJTF.insets = new Insets(0, 0, 5, 5);
		gbc_NewProfFNameJTF.fill = GridBagConstraints.HORIZONTAL;
		gbc_NewProfFNameJTF.gridx = 3;
		gbc_NewProfFNameJTF.gridy = 1;
		newProfJP.add(newProfFNameJTF, gbc_NewProfFNameJTF);
		newProfFNameJTF.setColumns(10);
		
		JLabel createLNameJL = new JLabel("Last Name");
		createLNameJL.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_createLNameJL = new GridBagConstraints();
		gbc_createLNameJL.anchor = GridBagConstraints.EAST;
		gbc_createLNameJL.insets = new Insets(0, 0, 5, 5);
		gbc_createLNameJL.gridx = 2;
		gbc_createLNameJL.gridy = 2;
		newProfJP.add(createLNameJL, gbc_createLNameJL);
		
		newProfLNameJTF = new JTextField();
		GridBagConstraints gbc_NewProfLNameJTF = new GridBagConstraints();
		gbc_NewProfLNameJTF.insets = new Insets(0, 0, 5, 5);
		gbc_NewProfLNameJTF.fill = GridBagConstraints.HORIZONTAL;
		gbc_NewProfLNameJTF.gridx = 3;
		gbc_NewProfLNameJTF.gridy = 2;
		newProfJP.add(newProfLNameJTF, gbc_NewProfLNameJTF);
		newProfLNameJTF.setColumns(10);
		
		btnAddProfile = new JButton("Add New Profile");
		btnAddProfile.addActionListener(new btnListener());
		GridBagConstraints gbc_btnAddProfile = new GridBagConstraints();
		gbc_btnAddProfile.insets = new Insets(0, 0, 5, 5);
		gbc_btnAddProfile.gridx = 5;
		gbc_btnAddProfile.gridy = 2;
		newProfJP.add(btnAddProfile, gbc_btnAddProfile);
		
		JLabel createHtownJL = new JLabel("Hometown");
		createHtownJL.setHorizontalAlignment(SwingConstants.RIGHT);
		GridBagConstraints gbc_createHtownJL = new GridBagConstraints();
		gbc_createHtownJL.anchor = GridBagConstraints.EAST;
		gbc_createHtownJL.insets = new Insets(0, 0, 5, 5);
		gbc_createHtownJL.gridx = 2;
		gbc_createHtownJL.gridy = 3;
		newProfJP.add(createHtownJL, gbc_createHtownJL);
		
		newProfHtownJTF = new JTextField();
		GridBagConstraints gbc_newProfHtownJTF = new GridBagConstraints();
		gbc_newProfHtownJTF.insets = new Insets(0, 0, 5, 5);
		gbc_newProfHtownJTF.fill = GridBagConstraints.HORIZONTAL;
		gbc_newProfHtownJTF.gridx = 3;
		gbc_newProfHtownJTF.gridy = 3;
		newProfJP.add(newProfHtownJTF, gbc_newProfHtownJTF);
		newProfHtownJTF.setColumns(10);
		
		JPanel panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 0, 5);
		gbc_panel.gridwidth = 2;
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 1;
		gbc_panel.gridy = 4;
		add(panel, gbc_panel);
		
		btnExit = new JButton("Exit");
		btnExit.addActionListener(new btnListener());
		btnExit.setAlignmentX(Component.CENTER_ALIGNMENT);
		btnExit.setHorizontalTextPosition(SwingConstants.CENTER);
		panel.add(btnExit);
		
	}
	
	/**
	 * method to fill in the ComboBox and JList on first run
	 */
	private void populate() {
		// TODO Implement code that will read the variables instantiated from DataManager.java
		// Profile Loading into JComboBox
		String[] profileTemp = dm.getProfile();
		for (String e : profileTemp) {
			profileJCB.addItem(dm.toStringProfile(e));
		}
	}
	
	/**
	 * Class for JComboBox listener
	 * 
	 * @author Terry Wasserleben CS204-21370 Prof. Jeannette Myers Due:
	 *         11/21/2011
	 */
	public class boxListener implements ActionListener {
		
		/**
		 * method reads object source and then uses that source to get the
		 * selected item
		 */
		public void actionPerformed(ActionEvent event2) {
			JComboBox jcb = (JComboBox) event2.getSource();
			profileSelected = (String) jcb.getSelectedItem();
			populate();
		}
	}
	
	/**
	 * Class for button listener
	 * 
	 * @author Terry Wasserleben CS204-21370 Prof. Jeannette Myers Due:
	 *         11/21/2011
	 */
	public class btnListener implements ActionListener {
		
		/**
		 * method reads object source and based on the source executes an action
		 */
		@Override
		public void actionPerformed(ActionEvent event1) {
			Object source = event1.getSource();
			if (source == btnExit) {
				System.exit(0);
			} else if (source == btnShowFriends) {

			} else if (source == btnAddFriend) {

			} else if (source == btnAddProfile) {
				dm.addProfile(newProfFNameJTF.getText(), newProfLNameJTF
						.getText(), newProfHtownJTF.getText());
			}
		}
		
	}
	
}
